package com.lc.recovery.ui.activity.recoverymine;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.aop.SingleClick;
import com.lc.recovery.app.AppActivity;
import com.lc.recovery.http.request.CashDoApi;
import com.lc.recovery.http.request.CashInfoApi;
import com.lc.recovery.http.response.CashInfoBean;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.util.BtnUtils;
import com.lc.recovery.util.SPUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithdrawalActivity extends AppActivity {

    @BindView(R.id.tv_chae)
    TextView tvChae;
    @BindView(R.id.tv_dangqianshouyi)
    TextView tvDangqianshouyi;
    @BindView(R.id.iv_weixin_iv)
    ImageView ivWeixinIv;
    @BindView(R.id.rl_weixin)
    RelativeLayout rlWeixin;
    @BindView(R.id.tv_rule)
    TextView tvRule;
    @BindView(R.id.et_balance)
    EditText etBalance;
    private String cash_mony;
    private String user_type;
    private String userId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_withdrawal;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

        if (SPUtil.get(this, IntentKey.TYPE,"").equals("1")||SPUtil.get(this, IntentKey.TYPE,"").equals("2")){
            user_type = "1";
        }else if (SPUtil.get(this, IntentKey.TYPE,"").equals("3")){
            user_type="2";
        }else {
            user_type="3";
        }
        userId=SPUtil.get(this, IntentKey.USERID,"");
        cashInfo();
    }

    private void cashInfo() {

        EasyHttp.post(this)
                .api(new CashInfoApi().setUser_id(userId).setType(user_type)).request(new HttpCallback<CashInfoBean>(this) {

            @Override
            public void onSucceed(CashInfoBean data) {
                if (data.getCode() == 200) {
                    tvDangqianshouyi.setText("当前收益：" + data.getMoney());
                    tvChae.setText("最低提现额度：" + data.getCash_mony() + "元");
                    cash_mony = data.getCash_mony();
                    tvRule.setText(Html.fromHtml(data.getRule()));
                }

            }

            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }

    private void cashDo() {
        if (etBalance.getText().toString().isEmpty()){
            toast("请输入提现金额");
            return;
        }
          if (cash_mony!=null){
              if (Double.parseDouble(etBalance.getText().toString())<Double.parseDouble(cash_mony)){
               toast("最低提现额度："+cash_mony+"元");
               return;
              }
          }

        EasyHttp.post(this)
                .api(new CashDoApi().setUser_id(SPUtil.get(getContext(), IntentKey.USERID, "")).setUser_type(user_type).setAmount(etBalance.getText().toString())).request(new HttpCallback<CashInfoBean>(this) {

            @Override
            public void onSucceed(CashInfoBean data) {
                if (data.getCode() == 200) {
                    toast(data.getMsg());
                    finish();
                }else {
                    toast(data.getMsg());
                }

            }

            @Override
            public void onFail(Exception e) {
//           /     super.onFail(e);

            }
        });

    }
   @SingleClick
    @OnClick({R.id.tv_yuejilu, R.id.rl_weixin, R.id.tv_tixian})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_yuejilu:
                startActivity(WithdrawalRecordActivity.class);
                break;
            case R.id.rl_weixin:
                break;
            case R.id.tv_tixian:
                if (BtnUtils.isFastClick()) {
                    cashDo();
                }
                break;
        }
    }


}
